// pages/solarclassification/solarclassification.js
var utils = require('../../utils/util.js');
Page({
  data: {
    currentPage: 1,
    type: 0
  },
  onLoad: function(options) {
    var that = this;
    that.setData({
      type: options.type ? options.type : 0,
      banner: options.banner,
      id: options.id,
    //   number: wx.getStorageSync("number")
    })
    that.goodslist(that.data.currentPage);
    that.memberInfo();
  },
  memberInfo: function() {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      that.setData({
        memberInfo: res.datas
      })
    });
  },
  onShow: function(e) {
	  var that = this;
	  var cartInfo = wx.getStorageSync("cartInfo") ? wx.getStorageSync("cartInfo") : [];
	  var number = 0;
	  cartInfo.forEach((value, index) => {
		  number = number + value.number;
	  })
	  that.setData({
		  number: number,
	  });
  },
  lll: function(e) {
    var that = this;
    if (that.data.allListData != undefined) {
      var allListData = that.data.allListData;
      for (var i in allListData) {
        allListData[i].number = 0;
        for (var k in wx.getStorageSync("listPost")) {
          if (wx.getStorageSync("listPost")[k].id == allListData[i].id) {
            allListData[i].number = wx.getStorageSync("listPost")[k].number;
          }
        }
      }
      that.setData({
        allListData: allListData
      })
      console.log(that.data.allListData);
    }
  },
  //所有的商品列表
  goodslist: function(currentPage) {
    var that = this;
    utils.showLoading();
    var url = that.data.type == 0 ? '/api/v1/index/activity_goods' : '/api/v1/goods/goods_list';
    var param = {
      page: currentPage
    };
    if (that.data.type == 0) {
      param.activity_id = that.data.id;
    } else {
      //0元尝鲜的时候
      param.if_zero = 1;
      param.if_advance = 0;
    }
    utils.http(url, param, function(res) {
      utils.hideLoading();
      console.log(res.datas);
      var allpost = that.data.allListData;
      if (allpost == undefined) {
        allpost = [];
      }
      for (var i in res.datas) {
        res.datas[i].number = 0;
        allpost.push(res.datas[i])
      }
      if (res.datas.length < 10) {
        that.setData({
          tishi_title: '已加载全部',
        });
      } else {
        that.setData({
          tishi_title: '加载更多',
        });
      }
      that.setData({
        allListData: allpost,
        ready: res.datas.length == 0 ? 1 : 2,
      })
      that.lll();
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (that.data.ready == 2) {
      var page = that.data.currentPage + 1
      that.setData({
        currentPage: page,
      })
      that.goodslist(page)
    }
  },
  //加数量
  onPrevTap: function(e) {
    var that = this;
    var itemslist = that.data.allListData;
    var index = e.currentTarget.dataset.index;
    var num = e.currentTarget.dataset.number;
    itemslist[index].number = itemslist[index].number + 1;
    that.setData({
      allListData: itemslist
    })
    var shuzi = wx.getStorageSync("number") || 0;
    var number = shuzi + 1
    that.setData({
      number: number
    })
    wx.setStorageSync("number", number);
    var dataInfo = {
      id: e.currentTarget.dataset.id,
      image: e.currentTarget.dataset.image,
      name: e.currentTarget.dataset.name,
      price: e.currentTarget.dataset.price,
      subtitle: e.currentTarget.dataset.subtitle,
      vip_price: e.currentTarget.dataset.vipprice,
      selectStatus: false, //购物车做删除时候用
      number: 1 //加减数量
    }
    utils.uniqueSearchRecord(dataInfo);
  },
  //减数量
  onNextTap: function(e) {
    var that = this;
    var itemslist = that.data.allListData;
    var index = e.currentTarget.dataset.index;
    var ind = e.currentTarget.dataset.ind;
    if (itemslist[index].number > 0) {
      itemslist[index].number = itemslist[index].number - 1;
      var nowStorage = wx.getStorageSync("listPost");
      for (var i in nowStorage) {
        if (nowStorage[i].id == itemslist[index].id) {
          nowStorage[i].number = nowStorage[i].number - 1;
          if (nowStorage[i].number == 0) {
            nowStorage.splice(i, 1);
          }
        }
      }
      that.setData({
        number: wx.getStorageSync("number") - 1,
        allListData: itemslist
      })
      wx.setStorageSync("listPost", nowStorage)
      wx.setStorageSync("number", that.data.number);
    } else {
      utils.message("该宝贝宝贝不能减少哦~")
    }

  },
  //跳入购物车
  onCarShoppTap: function(e) {
    console.log(e);
    wx.switchTab({
      url: '../cartshopp/cartshopp'
    })
  },
  //商品详情
  noFrutisDetailsTap: function(e) {
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '../commoditydetails/commoditydetails?id=' + id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})